package com.projectxxxxxxxxxx.animator;

import java.util.TimerTask;

import android.content.Context;
import android.view.View;
import android.widget.RemoteViews;

import com.projectxxxxxxxxxx.R;
import com.projectxxxxxxxxxx.screens.HardwareScreen.LabelParams;
import com.projectxxxxxxxxxx.screens.Screen;
import com.projectxxxxxxxxxx.widget.WidgetManager;
import com.projectxxxxxxxxxx.widget.WidgetView;

public class ScreenAnimator extends Animator {
    private final long WIDGET_UPDATE_INTERVAL = 10;
    public static final int WIDGET_NEXT_SCREEN = 1;
    public static final int WIDGET_PREV_SCREEN = 2;

    private static int previosScreen;

    private static int steps;

    private final int[] viewsIds = {
            R.id.shield2,
            R.id.shield3,
            R.id.shield4,
            R.id.shield5,
            R.id.shield6,
            R.id.shield7,
            R.id.shield8,
            R.id.shield9,
            R.id.shield10,
            R.id.shield11,
            R.id.shield12,
            R.id.shield13,
            R.id.shield14,
            R.id.shield15,
            R.id.shield16,
            R.id.shield17,
            R.id.shield18,
            R.id.shield19,
            R.id.shield20,
            R.id.shield21,
            R.id.shield22,
            R.id.shield23,
            R.id.shield24,
            R.id.shield25,
            R.id.shield26,
            R.id.shield27,
            R.id.shield28,
            R.id.shield29,
            R.id.shield30,
            R.id.shield31,
            R.id.shield32,
            R.id.shield33,
            R.id.shield34,
            R.id.shield35,
            R.id.shield36,
            R.id.shield37,
            R.id.shield38,
            R.id.shield39,
            R.id.shield40,
            R.id.shield42,
            R.id.shield43,
            R.id.shield44,
            R.id.shield45,
            R.id.shield46,
            R.id.shield47,
            R.id.shield48,
            R.id.shield49,
            R.id.shield50,
            R.id.shield51,
            R.id.shield52,
            R.id.shield53,
            R.id.shield54,
            R.id.shield55,
            R.id.shield56,
            R.id.shield57,
            R.id.shield58,
            R.id.shield59,
            R.id.shield60,
            R.id.shield61,
            R.id.shield62,
            R.id.shield63,
            R.id.shield64,
            R.id.shield65,
            R.id.shield66,
            R.id.shield67,
            R.id.shield68,
            R.id.shield69,
            R.id.shield70,
            R.id.shield71,
            R.id.shield72,
            R.id.shield73,
            R.id.shield74,
            R.id.shield75,
            R.id.shield76,
            R.id.shield77,
            R.id.shield78,
            R.id.shield79,
            R.id.shield80,
            R.id.shield81,
            R.id.shield82,
            R.id.shield83,
            R.id.shield84,
            R.id.shield85,
            R.id.shield86,
            R.id.shield87,
            R.id.shield88,
            R.id.shield89,
            R.id.shield90,
            R.id.shield91,
            R.id.shield92,
            R.id.shield93,
            R.id.shield94,
            R.id.shield95,
            R.id.shield96,
            R.id.shield97,
            R.id.shield98,
            R.id.shield99,
            R.id.shield100,
            R.id.shield101,
            R.id.shield102,
            R.id.shield103,
            R.id.shield104,
            R.id.shield105,
            R.id.shield106,
            R.id.shield107,
            R.id.shield108,
            R.id.shield109,
            R.id.shield110,
            R.id.shield111,
            R.id.shield112,
            R.id.shield113,
            R.id.shield114,
            R.id.shield115,
            R.id.shield116,
            R.id.shield117,
            R.id.shield118,
            R.id.shield119,
            R.id.shield120,
            R.id.shield121,
            R.id.shield122,
            R.id.shield123,
            R.id.shield124,
            R.id.shield125,
            R.id.shield126,
            R.id.shield127,
            R.id.shield128,
            R.id.shield129,
            R.id.shield130,
            R.id.shield131,
            R.id.shield132,
            R.id.shield133,
            R.id.shield134,
            R.id.shield135,
            R.id.shield136,
            R.id.shield137,
            R.id.shield138,
            R.id.shield139,
            R.id.shield140,
            R.id.shield142,
            R.id.shield143,
            R.id.shield144,
            R.id.shield145,
            R.id.shield146,
            R.id.shield147,
            R.id.shield148,
            R.id.shield149,
            R.id.shield150,
            R.id.shield151,
            R.id.shield152,
            R.id.shield153,
            R.id.shield154,
            R.id.shield155,
            R.id.shield156,
            R.id.shield157,
            R.id.shield158,
            R.id.shield159,
            R.id.shield160,
            R.id.shield161,
            R.id.shield162,
            R.id.shield163,
            R.id.shield164,
            R.id.shield165,
            R.id.shield166,
            R.id.shield167,
            R.id.shield168,
            R.id.shield169,
            R.id.shield170,
    };

    public ScreenAnimator(final Context context, final RemoteViews views, final int ind) {
        super(context, views);

        previosScreen = Screen.currentScreen;

        switch (ind) {
            case WIDGET_NEXT_SCREEN:
                toogleScreenToNext();
                break;
            case WIDGET_PREV_SCREEN:
                toogleScreenToPrev();
                break;
        }

        steps = 0;
    }

    private void toogleScreenToNext() {
        if (Screen.currentScreen == screenIds.length - 1) {
            Screen.currentScreen = 0;
        } else {
            Screen.currentScreen += 1;
        }

        // show shield from right side
        animatorStep = viewsIds.length / 2;
        views.setViewVisibility(R.id.shield_from_left, View.GONE);
        views.setViewVisibility(R.id.shield_from_right, View.VISIBLE);
    }

    private void toogleScreenToPrev() {
        if (Screen.currentScreen == 0) {
            Screen.currentScreen = screenIds.length - 1;
        } else {
            Screen.currentScreen -= 1;
        }

        // show shield from left side
        animatorStep = 0;
        views.setViewVisibility(R.id.shield_from_right, View.GONE);
        views.setViewVisibility(R.id.shield_from_left, View.VISIBLE);
    }

    @Override
    public synchronized void animate(final RemoteViews views, final LabelParams label) {

        animatorStep += 1;
        steps += 1;

        // cancel animation
        if (animatorStep >= viewsIds.length || steps >= viewsIds.length / 2) {
            pingTimer.cancel();

            views.setViewVisibility(R.id.shield_from_left, View.GONE);
            views.setViewVisibility(R.id.shield_from_right, View.GONE);

            for (int i = 0; i < viewsIds.length; i++) {
                views.setViewVisibility(viewsIds[i], View.GONE);
            }

            WidgetView.animator = null;
            animatorStep = 0;

            return;
        }

        // change screen
        int divider = steps - 10;
        if (divider > 0) {
            if ((viewsIds.length / divider) == 4) {
                views.setViewVisibility(screenIds[previosScreen], View.GONE);
                views.setViewVisibility(screenIds[Screen.currentScreen], View.VISIBLE);
            }
        }
        
        // animation of shield
        views.setViewVisibility(viewsIds[animatorStep - 1], View.GONE);	
        	
        if(animatorStep - 1 == 12 || animatorStep - 1 == 25){
        	views.setViewVisibility(viewsIds[animatorStep - 1], View.VISIBLE);
        }

        views.setViewVisibility(viewsIds[animatorStep], View.VISIBLE);

        if(animatorStep == 49){
        	views.setViewVisibility(viewsIds[74], View.VISIBLE);
        	views.setViewVisibility(viewsIds[61], View.VISIBLE);
        	views.setViewVisibility(viewsIds[48], View.VISIBLE);
        	
        	views.setViewVisibility(viewsIds[12], View.GONE);
        	views.setViewVisibility(viewsIds[25], View.GONE);
        	views.setViewVisibility(viewsIds[48], View.GONE);
        	//return;
        }
        
        
        // every time need start timer
        pingTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                requestRedraw(WidgetManager.ACTION_WIDGET_UPDATE);
            }
        }, WIDGET_UPDATE_INTERVAL);

    }
}
